package com.example.demo.redis;

import com.alibaba.fastjson.JSON;
import com.example.demo.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangxinju@hellobike.com
 * @date 2019-08-20 16:36
 **/

@Service
public class RedisClientImpl implements RedisClient {

    private final StringRedisTemplate redisTemplate;

    @Autowired
    public RedisClientImpl(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void set() {
        Map<String, String> jsonMap = new HashMap<>(10);
        jsonMap.put("name","Jack");
        jsonMap.put("age","10");
        redisTemplate.opsForValue().set("Student", JSON.toJSONString(jsonMap));

        LogUtils.COMMON.info("set redis value:{}", JSON.toJSONString(jsonMap));

        String student = redisTemplate.opsForValue().get("Student");

        redisTemplate.opsForHash().putAll("teacher", jsonMap);

        LogUtils.COMMON.info("get redis value:{}", student);
    }

}
